package com.jie.demo1.controller.enRollContorller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jie.demo1.Sservice.CaptchaVoService;
import com.jie.demo1.Sservice.UserService;
import com.jie.demo1.common.ulity.interfaceU.imageIdentifyService;
import com.jie.demo1.common.ulity.securityUtil;
import com.jie.demo1.pojo.Captcha.CaptchaVo;
import com.jie.demo1.pojo.Result;
import com.jie.demo1.pojo.User;
import com.jie.demo1.pojo.UserVo;
import jakarta.annotation.Resource;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.jie.demo1.pojo.menu.ResultCodeEnum.ADMIN_ACCOUNT_EXIST_ERROR;

@RestController
@RequestMapping("/register")
@CrossOrigin(origins = "http://localhost:5173/")  //开启跨域请求
public class registerController {
    @Autowired
    private RedisTemplate redisTemplate;

    @Resource
    private UserService userService;

    @Autowired
    private imageIdentifyService imageIdentify;

    @Autowired
    private CaptchaVoService captchaVoService;

    //用于注册等相关的接口
    @PostMapping("/registerUser")
    public Result registerUser(@RequestBody User user) {

        //检查用户是否存在
        LambdaQueryWrapper<User> userWrapper = new LambdaQueryWrapper<>();
        userWrapper.eq(User::getName,user.getName());
        List<User> userList = userService.list(userWrapper);
        if (userList.size() > 0) {
            return Result.fail(301, "用户已存在");
        }
        //将用户插入到数据库
        //处理密码，使用单项加密后保存到user对象中
        String password = user.getPassword();
        //加密密码，使用md5加密
        password = securityUtil.md5Hex(password);
        user.setPassword(password);
        userService.saveOrUpdate(user);
        return Result.ok(); // 注册成功,无需返回用户信息，返回空对象即可
    }

    //生成图片验证码接口，暂时存放在数据库，用作验证
    @PostMapping("/getImageCode")
    public Result<CaptchaVo> getImageCode() {
        CaptchaVo captchaVo = imageIdentify.GetCaptServer();//返回一个captchaVo对象，包含key和base64的string
        System.out.println("-----------"+captchaVo.getKeyI());
        Boolean s =captchaVoService.saveImageBase64(captchaVo);
        //返回captchaVo对象给前端前端将这个base64的string展示出来，key则会通过前端传递给后端的同一个页面接口，后端通过key来验证验证码是否正确
        return Result.ok(captchaVo);

    }


    //找回密码功能接口
    @PostMapping("/recoverPassword")
    public Result<User>  recoverPassword(@RequestBody UserVo uservo){
        //检查用户是否存在，通过用户名和电话（邮箱）查找用户，然后重新设置密码，并更新用户信息
        LambdaUpdateWrapper<User> userUpdaterWrapper = new LambdaUpdateWrapper<>();
        userUpdaterWrapper = userUpdaterWrapper.eq(User::getName, uservo.getName())
                .eq(User::getPhone, uservo.getPhone()).eq(User::getIsValid, "Y");

        List<User> userList = userService.list(userUpdaterWrapper);
        if (userList == null || userList.isEmpty()) {
            return Result.fail(301, "用户不存在或者已经被禁用");
        }

        //code是前端传入的验证码，需要与redis中的验证码进行比对
        if (uservo.getCode() == null ){
            return Result.fail(301, "验证码错误");
        }
        String o = (String) redisTemplate.opsForValue().get(uservo.getKeyI());
        //判断验证码是否过期
        if (o == null){
            //验证码过期
            return Result.fail(301, "验证码过期");
        }

        //判断验证码是否正确,不区分大小写
        if (! o.equals(uservo.getCode().toLowerCase())) {
            return Result.fail(301, "验证码错误");
        }
        //验证码正确，更新用户密码
        User user = userList.get(0);
        //处理密码，使用单项加密后保存到user对象中
        String password = securityUtil.md5Hex(user.getPassword());
        userUpdaterWrapper.set(User::getPassword, password);
        userService.update(userUpdaterWrapper);
        return Result.ok(user);
    }

}